
#include <iostream>
#include <windows.h>
#include "modules/family.h"
#include "modules/calculate.cpp"
#include "modules/insert.cpp"
#include "modules/delete.cpp"
#include "modules/search.cpp"
#include "modules/modify.cpp"
#include "modules/sort.cpp"
#include "modules/display.cpp"
#include "modules/traversal.cpp"
#include "modules/read.cpp"

void displayMenu()
{
    std::cout << "欢迎来到家谱管理系统" << std::endl;
    std::cout << "1. 显示家谱图" << std::endl;
    std::cout << "2. 层次遍历家谱图示第n代人的所有信息" << std::endl;
    std::cout << "3. 按照姓名查询，输出成员信息" << std::endl;
    std::cout << "4. 按照出生日期查询成员名单" << std::endl;
    std::cout << "5. 确定两人姓名的关系" << std::endl;
    std::cout << "6. 给某成员添加孩子" << std::endl;
    std::cout << "7. 删除某成员" << std::endl;
    std::cout << "8. 修改某成员信息" << std::endl;
    std::cout << "9. 按出生日期对家谱中所有人排序" << std::endl;
    std::cout << "10. 先序遍历树并输出" << std::endl;
    std::cout << "11. 后序遍历树并输出" << std::endl;
    std::cout << "12. 中序遍历树并输出" << std::endl;
    std::cout << "13. 建立二叉排序树，并按年龄大小排序并输出" << std::endl;
    std::cout << "14. 读取CSV文件并输出" << std::endl;
    std::cout << "15. 计算树的高度" << std::endl;
    std::cout << "16. 检查树是否平衡" << std::endl;
    std::cout << "17. 退出系统" << std::endl;
}

int main()
{
    SetConsoleOutputCP(65001);
    FamilyTree familyTree;
    int choice;
    familyTree.readCSV("data/data.csv"); // 读取data/data.csv

    while (true)
    {
        displayMenu();
        std::cout << "请输入您的选择: ";
        std::cin >> choice;
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

        switch (choice)
        {
        case 1:
            familyTree.displayTree();
            break;
        case 2:
        {
            int n;
            std::cout << "请输入代数 n: ";
            std::cin >> n;
            familyTree.displayNthGeneration(n - 1);
            break;
        }
        case 3:
        {
            std::string name;
            std::cout << "请输入成员姓名: ";
            std::getline(std::cin, name); // 使用std::getline读取姓名

            FamilyMember *member = familyTree.findMemberByName(name);
            if (member)
            {
                std::cout << "姓名: " << member->name
                          << ", 出生日期: " << member->birthDate
                          << ", 联系方式: " << member->contactInfo
                          << ", 婚否: " << (member->isMarried ? "已婚" : "未婚")
                          << ", 健在否: " << (member->isAlive ? "健在" : "已故")
                          << ", 死亡日期: " << member->deathDate << std::endl;

                std::cout << "父母信息: " << std::endl;
                for (auto child : member->children)
                {
                    std::cout << "孩子姓名: " << child->name
                              << ", 出生日期: " << child->birthDate
                              << ", 联系方式: " << child->contactInfo << std::endl;
                }
            }
            else
            {
                std::cout << "未找到该成员。" << std::endl;
            }
            break;
        }
        case 4:
        {
            std::string birthDate;
            std::cout << "请输入出生日期: ";
            std::cin >> birthDate;
            familyTree.searchByBirthDate(birthDate);
            break;
        }
        case 5:
        {
            std::string name1, name2;
            std::cout << "请输入两人姓名: ";
            std::cin >> name1 >> name2;
            familyTree.determineRelationship(name1, name2);
            break;
        }
        case 6:
        {
            std::string parentName, motherName, childName, birthDate, contactInfo;
            std::cout << "请输入父亲姓名: ";
            std::getline(std::cin, parentName); // 使用 std::getline 读取整行输入

            std::cout << "请输入母亲姓名: ";
            std::getline(std::cin, motherName);

            std::cout << "请输入孩子姓名: ";
            std::getline(std::cin, childName);

            std::cout << "请输入孩子出生日期 (格式: YYYY-MM-DD): ";
            std::getline(std::cin, birthDate);

            std::cout << "请输入孩子联系方式: ";
            std::getline(std::cin, contactInfo);

            FamilyMember *child = new FamilyMember(childName, birthDate, contactInfo);
            FamilyMember *father = familyTree.findMemberByName(parentName);
            FamilyMember *mother = motherName.empty() ? nullptr : familyTree.findMemberByName(motherName);

            if (father == nullptr)
            {
                std::cerr << "未找到父亲成员: " << parentName << std::endl;
                delete child; // 如果父亲不存在，删除孩子对象以避免内存泄漏
            }
            else
            {
                familyTree.addChild(father, mother, child);
                std::cout << "孩子已添加到家谱中。" << std::endl;
            }
            break;
        }
        case 7:
        {
            std::string memberName;
            std::cout << "请输入要删除的成员姓名: ";
            std::getline(std::cin, memberName); // 使用 std::getline 读取整行输入
            std::cout << "memberName: " << memberName << std::endl;
            FamilyMember *member = familyTree.findMemberByName(memberName);
            if (member)
            {
                familyTree.deleteMember(member);
                std::cout << "成员已从家谱中删除。" << std::endl;
            }
            else
            {
                std::cout << "未找到该成员。" << std::endl;
            }
            break;
        }
        case 8:
        {
            std::string memberName, newName, newBirthDate, newContactInfo, newIsMarried, newIsAlive, newDeathDate, newFatherName, newMotherName;
            std::cout << "请输入要修改的成员姓名: ";
            std::getline(std::cin, memberName);
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // 清除之前的换行符

            std::cout << "请输入新的名字 (留空不修改): ";
            std::getline(std::cin, newName);

            std::cout << "请输入新的出生日期 (留空不修改, 格式: YYYY-MM-DD): ";
            std::getline(std::cin, newBirthDate);

            std::cout << "请输入新的联系方式 (留空不修改): ";
            std::getline(std::cin, newContactInfo);

            std::cout << "请输入新的婚姻状况 (留空不修改, Yes/No): ";
            std::getline(std::cin, newIsMarried);

            std::cout << "请输入新的生存状况 (留空不修改, Yes/No): ";
            std::getline(std::cin, newIsAlive);

            std::cout << "请输入新的死亡日期 (留空不修改, 格式: YYYY-MM-DD): ";
            std::getline(std::cin, newDeathDate);

            std::cout << "请输入新的父亲姓名 (留空不修改): ";
            std::getline(std::cin, newFatherName);

            std::cout << "请输入新的母亲姓名 (留空不修改): ";
            std::getline(std::cin, newMotherName);

            familyTree.modifyMember(familyTree.findMemberByName(memberName), newName, newBirthDate, newContactInfo, newIsMarried, newIsAlive, newDeathDate, newFatherName, newMotherName);
            break;
        }
        case 9:
            familyTree.sortMembersByBirthDate();
            break;
        case 10:
            familyTree.preOrderTraversal(familyTree.getRoot());
            break;
        case 11:
            familyTree.postOrderTraversal(familyTree.getRoot());
            break;
        case 12:
            familyTree.inOrderTraversal(familyTree.getRoot());
            break;
        case 13:
            familyTree.sortMembersByAge();
            break;
        case 14:
        {
            std::string filename;
            std::cout << "请输入CSV文件名: ";
            std::cin >> filename;
            familyTree.readCSV(filename);
            break;
        }
        case 15:
            std::cout << "树的高度: " << familyTree.calculateHeight(familyTree.getRoot()) << std::endl;
            break;
        case 16:
            std::cout << (familyTree.isBalanced(familyTree.getRoot()) ? "树是平衡的" : "树是不平衡的") << std::endl;
            break;
        case 17:
            std::cout << "退出系统" << std::endl;
            return 0;
        default:
            std::cout << "无效的选择，请重试。" << std::endl;
        }
    }

    return 0; // 返回 0 表示程序正常结束
}